//
//  CXHTopOnSplashCustomEvent.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/10.
//

#import "CXHTopOnSplashCustomEvent.h"
#import "CXHTopOnCommonBiddingRequest.h"
#import "CXHTopOnCommonBiddingManager.h"

@implementation CXHTopOnSplashCustomEvent

/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    [self trackSplashAdClick];
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    [self trackSplashAdClosed:nil];
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    [self trackSplashAdShow];
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"===广告加载成功===");
    if (!self.isC2SBiding) {
        [self trackSplashAdLoaded:splashAd adExtra:nil];
        return;
    }
    CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:self.UUID];
    NSInteger price = splashAd.getBidPrice;
//    price = 100001;
    NSLog(@"===bid广告加载成功，价格为：%ld===",price);
    if (request.bidCompletion) {
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID unitGroupUnitID:request.unitGroup.unitID adapterClassString:request.unitGroup.adapterClassString price:@(price/100.0f).stringValue currencyType:ATBiddingCurrencyTypeCNY expirationInterval:request.unitGroup.networkTimeout customObject:splashAd];
        request.bidCompletion(bidInfo, nil);
    }
    
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    [self trackSplashAdLoadFailed:error];
    if (!self.isC2SBiding) {
        return;
    }
    CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:self.UUID];
    if (request.bidCompletion) {
        request.bidCompletion(nil, error);
    }
    [[CXHTopOnCommonBiddingManager sharedInstance] removeRequestItemWithUnitID:self.UUID];
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    [self trackSplashAdShowFailed:error];
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
    [self trackSplashAdDetailClosed];
}

- (NSString *)networkUnitId {
    return self.serverInfo[@"slot_id"];
}

@end
